package com.lich.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.lich.controller.base.BaseController;
import com.lich.entity.AccessManager;
import com.lich.entity.vo.Page;
import com.lich.entity.vo.Result;

@Controller
@RequestMapping("/accessManager")
public class AccessManagerController extends BaseController{
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ModelAndView list(HttpServletRequest request ){
		return new ModelAndView("manager/accessManager/list");
	}
	
	

	@ResponseBody
	@RequestMapping(value="doList",method=RequestMethod.POST)
	public ModelAndView list(HttpServletRequest request,Page<AccessManager> page ){
		Result<Object> result = new Result<>();
		Page<AccessManager> list =null;
		try {
			list = accessManagerService.list(page);
			
			
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("获取报错信息失败");
		}
		result.setObject(list);
		result.setSuccess(true);
		result.setMessage("获取报错信息成功");
		
		
		return new ModelAndView(new MappingJackson2JsonView(),"result",result);
	}
	
	@ResponseBody
	@RequestMapping(value="add",method=RequestMethod.GET)
	public ModelAndView save(HttpServletRequest request,AccessManager accessManager){
		return new ModelAndView("manager/accessManager/add");
	}
	
	
	@ResponseBody
	@RequestMapping(value="doSave",method=RequestMethod.POST)
	public ModelAndView doSave(HttpServletRequest request,AccessManager accessManager){
		
		Result<Object> result = new Result<>();
		if (accessManager==null) {
			result.setMessage("请输入参数");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		if(accessManager.getLimitTimes()==null){
			result.setMessage("访问限制不能为空");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		if(StringUtils.isBlank(accessManager.getUrl())){
			result.setMessage("url不能为空！");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		accessManagerService.insert(accessManager);
		result.setMessage("保存成功");
		result.setSuccess(true);
		return new ModelAndView(new MappingJackson2JsonView(),"result",result);
	}
	
	@ResponseBody
	@RequestMapping(value="edit",method=RequestMethod.POST)
	public ModelAndView edit(HttpServletRequest request,AccessManager accessManager ){
		Result<Object> result = new Result<>();
		if (accessManager==null || accessManager.getId()==null) {
			result.setMessage("请输入id");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		if (accessManager.getLimitTimes()==null) {
			result.setMessage("限制次数不能为空！");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		if (accessManager.getUrl()==null) {
			result.setMessage("url 不能为空");
			result.setSuccess(false);
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
			
		}
		result.setMessage("更新成功");
		result.setSuccess(true);
		accessManagerService.updata(accessManager);
		
		return new ModelAndView(new MappingJackson2JsonView(),"result",result);
	}
	
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ModelAndView delete(HttpServletRequest request,AccessManager accessManager ){
		Result<Object> result = new Result<>();
		if (accessManager==null || accessManager.getId()==null) {
			result.setSuccess(false);
			result.setObject(0);
			result.setMessage("请输入参数");
			return new ModelAndView(new MappingJackson2JsonView(),"result",result);
		}
		
		
		result.setMessage("删除成功");
		result.setSuccess(true);
		result.setObject(accessManagerService.delete(accessManager));
		return new ModelAndView(new MappingJackson2JsonView(),"result",result);
	}
	
	
}
